package fr.umlv.escape_ig.game;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import fr.umlv.escape_ig.R;
import fr.umlv.escape_ig.manager.Display;
import fr.umlv.escape_ig.ship.Life;
import fr.umlv.escape_ig.ship.Player;
import fr.umlv.escape_ig.ship.Robot;
import fr.umlv.escape_ig.sprite.Sprite;
import fr.umlv.escape_ig.weapon.Ball;

public class GameState {

	public final Player player;
	private final ArrayList<Sprite> sprites = new ArrayList<Sprite>();
	private Bitmap backImg;
	private Bitmap backImg2;
	private double yScrolling;
	private double yScrolling2;
	Life life;
	Display display;
	DisplayMetrics ecran;
	boolean endGame = true;

	public GameState(Context context, DisplayMetrics ecran) {
		this.player = new Player(context, 256.0f, 700.0f);
		sprites.add(player);
		sprites.add(new Robot(context));
		sprites.add(new Robot(context, 512.0f, 15.0f));
		sprites.add(new Robot(context, 300.0f, 150.0f));
		sprites.add(new Robot(context, 420.0f, 350.0f));
		sprites.add(new Robot(context, 110.0f, 220.0f));

		backImg = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.background);
		backImg2 = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.background);
		life = new Life(6, context);
		display = new Display(context);
		this.ecran = ecran;
		yScrolling = 0;
		yScrolling2 = -backImg.getHeight();
	}

	public void computeNextFrame() {
		player.update();
		for (int i = 0; i < sprites.size(); i++) {
			sprites.get(i).update();
		}

		checkForCollisions();
		removeUselessSprites();
	}

	private void removeUselessSprites() {
		for (int i = 0; i < sprites.size(); i++) {
			if (sprites.get(i).shouldDisapear) {
				sprites.remove(i);
			}
		}
	}

	private void checkForCollisions() {
		for (int i = 0; i < sprites.size(); i++) {
			for (int j = 0; j < sprites.size(); j++) {
				if (i != j) {
					onCollide(sprites.get(i), sprites.get(j));
				}
			}
		}
	}

	public void drawFrame(Canvas canvas) {
		canvas.drawBitmap(backImg, 0, (float) yScrolling, null);
		canvas.drawBitmap(backImg2, 0, (float) yScrolling2, null);
		if (endGame) {
			yScrolling++;
			yScrolling2++;
			if (yScrolling == ecran.heightPixels) {
				yScrolling = -backImg.getHeight();
			}
			if (yScrolling2 == ecran.heightPixels) {
				yScrolling2 = -backImg2.getHeight();
			}
		}
		player.draw(canvas);
		for (int i = 0; i < sprites.size(); i++) {
			sprites.get(i).draw(canvas);
		}
		display.displayIcon(canvas);
		life.display(ecran, canvas);

	}

	public void playerLoadWeapon() {
		player.loadWeapon();
	}

	public void playerFire() {
		Ball b = player.fire();
		if (b != null) {
			sprites.add(b);
		}
	}

	private static void onCollide(Sprite a, Sprite b) {
		if (RectF.intersects(a.rect, b.rect)) {
			a.shouldDisapear = true;
			b.shouldDisapear = true;
		}
	}

}